package com.ray.util;

import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class RequestUtil {
	
	//获取所有request里的参数
	public static Map getRequestAllParam(HttpServletRequest request) throws ServiceException {
		MyUser myUser=RequestUtil.getLoginUser(request);
		Map map=new HashMap();
		Enumeration enu=request.getParameterNames();  
		while(enu.hasMoreElements()){  
			String paraName=(String)enu.nextElement();  
			map.put(paraName, request.getParameter(paraName));
		}
		map.put("loginCompany", myUser.getCompany());
		map.put("loginUserName", myUser.getUserName());
		map.put("loginUserId", myUser.getUserId());
		map.put("loginUserDeptId", myUser.getDeptId());
		map.put("loginUserPhone", myUser.getPhone());
		map.put("loginUserOpenId", myUser.getWeiXinPublicOpenId());
		map.put("loginCompanyId", myUser.getCompanyId());
		if(myUser.getCompanyInfo()!=null) {
			map.put("loginCompanyType", myUser.getCompanyInfo().getType());
		}
		return map;
	}
	
	
	/**
	 * 获取Request里面的所有参数,加泛型
	* @date 2018年4月26日 上午8:38:30 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	public static Map<String,Object> getRequestParamByGeneric(HttpServletRequest request) throws ServiceException {
		MyUser myUser=RequestUtil.getLoginUser(request);
		Map<String,Object> map= new HashMap<>();
		Enumeration<String> enu=request.getParameterNames();   
		while(enu.hasMoreElements()){  
			String paraName= enu.nextElement();
			map.put(paraName, request.getParameter(paraName));
		}
		map.put("loginCompany", myUser.getCompany());
		map.put("loginUserName", myUser.getUserName());
		map.put("loginUserId", myUser.getUserId());
		map.put("loginUserDeptId", myUser.getDeptId());
		map.put("loginUserPhone", myUser.getPhone());
		map.put("loginUserOpenId", myUser.getWeiXinPublicOpenId());
		map.put("loginCompanyId", myUser.getCompanyId());
		return map;
	}
	
	
	public static Map<String,String> getRequestAllParam2String(HttpServletRequest request) throws ServiceException {
		MyUser myUser=RequestUtil.getLoginUser(request);
		Map<String,String> map=new HashMap<>();
		Enumeration<String> enu=request.getParameterNames();  
		while(enu.hasMoreElements()){  
			String paraName=(String)enu.nextElement();  
			map.put(paraName, request.getParameter(paraName));
		}
		map.put("loginCompany", myUser.getCompany());
		map.put("loginUserName", myUser.getUserName());
		map.put("loginUserId", myUser.getUserId());
		map.put("loginUserDeptId", myUser.getDeptId());
		map.put("loginUserPhone", myUser.getPhone());
		map.put("loginUserOpenId", myUser.getWeiXinPublicOpenId());
		map.put("loginCompanyId", myUser.getCompanyId()+"");
		return map;
	}
	
	public static Map<String,String> getRequestAllParamNotLogin2String(HttpServletRequest request) throws ServiceException {
		Map<String,String> map=new HashMap<>();
		Enumeration<String> enu=request.getParameterNames();  
		while(enu.hasMoreElements()){  
			String paraName=(String)enu.nextElement();  
			map.put(paraName, request.getParameter(paraName));
		}
		return map;
	}
	
	//获取所有request里的参数
	public static Map getRequestAllParamNotLogin(HttpServletRequest request) throws ServiceException {
		Map map=new HashMap();
		Enumeration enu=request.getParameterNames();  
		while(enu.hasMoreElements()){  
			String paraName=(String)enu.nextElement();  
			map.put(paraName, request.getParameter(paraName));
		}
		return map;
	}
	
	//获取登录用户
	public static MyUser getLoginUser(HttpServletRequest request) throws ServiceException {
		MyUser myuser=(MyUser) request.getSession().getAttribute("userLogin");
		if(myuser==null) {
			throw new ServiceException("未登陆或已掉线"); 
		}
		return myuser;
	}

}
